package pers.qianyu.module.core.domain.system.converter;


import com.baomidou.mybatisplus.extension.plugins.pagination.*;
import com.google.common.collect.*;
import org.springframework.stereotype.*;
import pers.qianyu.module.core.comm.*;
import pers.qianyu.module.core.domain.system.dto.*;
import pers.qianyu.module.core.domain.system.entity.*;
import pers.qianyu.module.core.domain.system.enums.SysUserGenderEnum;
import pers.qianyu.module.core.domain.system.enums.SysUserStatusEnum;
import pers.qianyu.module.core.domain.system.enums.SysUserTypeEnum;
import pers.qianyu.module.core.domain.system.query.*;
import pers.qianyu.module.core.domain.system.vo.*;

import java.util.*;
import java.util.stream.*;

/**
 * @author mizzle rain
 * @date 2020-10-28 13:22
 */
@Component
public class SysUserConverter extends BaseConverter {
    /**
     * 将 po 对象装换为 vo 对象
     *
     * @param sysUserPO         用户 po 对象
     * @param sysRoleVOS        角色 vo 对象列表
     * @param sysResourceVOS    资源 vo 对象列表
     * @param imageLikeIds      所有点赞过的图片id
     * @param myFollowedUserIds 该用户关注的用户的 id
     * @return 对应的 vo 对象
     */
    public SysUserVO convertToVO(SysUserPO sysUserPO,
                                 List<SysRoleVO> sysRoleVOS,
                                 List<SysResourceVO> sysResourceVOS,
                                 List<Long> imageLikeIds,
                                 List<Long> imageCollectionIds,
                                 Set<Long> myFollowedUserIds
    ) {
        SysUserVO sysUserVO = convertToVO(sysUserPO);
        sysUserVO.setResources(Sets.newLinkedHashSet(sysResourceVOS));
        sysUserVO.setRoles(Sets.newLinkedHashSet(sysRoleVOS));
        sysUserVO.setImageLikeIds(new LinkedHashSet<>(imageLikeIds));
        sysUserVO.setImageCollectionIds(new LinkedHashSet<>(imageCollectionIds));
        sysUserVO.setFollowedUserIds(new LinkedHashSet<>(myFollowedUserIds));
        return sysUserVO;
    }

    public SysUserVO convertToVO(SysUserPO sysUserPO) {
        SysUserVO userVO = new SysUserVO();
        userVO.setUsername(sysUserPO.getUsername());
        userVO.setStatus(sysUserPO.getStatus());
        userVO.setNickname(sysUserPO.getNickname());
        userVO.setType(sysUserPO.getType());
        userVO.setEmail(sysUserPO.getEmail());
        userVO.setPhone(sysUserPO.getPhone());
        userVO.setGender(sysUserPO.getGender());
        userVO.setAvatar(sysUserPO.getAvatar());
        userVO.setRemark(sysUserPO.getRemark());
        entityToVO(sysUserPO, userVO);
        return userVO;
    }

    /**
     * 将 dto 对象装换为 po 对象
     *
     * @param sysUserDTO dto 对象
     * @return 对应的 po 对象
     */
    public SysUserPO convertToPO(SysUserDTO sysUserDTO) {
        SysUserPO sysUserPO = new SysUserPO();
        sysUserPO.setUsername(sysUserDTO.getUsername());
        sysUserPO.setPassword(sysUserDTO.getPassword());
        sysUserPO.setNickname(sysUserDTO.getNickname());
        sysUserPO.setType(Optional.ofNullable(sysUserDTO.getType()).orElse(SysUserTypeEnum.COMM_USER.getCode()));
        sysUserPO.setEmail(sysUserDTO.getEmail());
        sysUserPO.setPhone(sysUserDTO.getPhone());
        sysUserPO.setGender(Optional.ofNullable(sysUserDTO.getGender()).orElse(SysUserGenderEnum.UNKNOWN.getCode()));
        sysUserPO.setAvatar(sysUserDTO.getAvatar());
        sysUserPO.setStatus(Optional.ofNullable(sysUserDTO.getStatus()).orElse(SysUserStatusEnum.NORMAL.getCode()));
        sysUserPO.setRemark(sysUserDTO.getRemark());
        return sysUserPO;
    }

    /**
     * 将 dto 对象装换为 po 对象
     *
     * @param sysUserForUpdateDTO dto 对象
     * @return 对应的 po 对象
     */
    public SysUserPO convertToPO(SysUserForUpdateDTO sysUserForUpdateDTO) {
        SysUserPO sysUserPO = new SysUserPO();
        sysUserPO.setId(sysUserForUpdateDTO.getId());
        sysUserPO.setNickname(sysUserForUpdateDTO.getNickname());
        sysUserPO.setEmail(sysUserForUpdateDTO.getEmail());
        sysUserPO.setPhone(sysUserForUpdateDTO.getPhone());
        sysUserPO.setGender(Optional.ofNullable(sysUserForUpdateDTO.getGender()).orElse(2));
        sysUserPO.setAvatar(sysUserForUpdateDTO.getAvatar());
        return sysUserPO;
    }

    /**
     * 将 query 对象装换为 po 对象
     *
     * @param sysUserQuery query 对象
     * @return po 对象
     */
    public SysUserPO convertToPO(SysUserQuery sysUserQuery) {
        SysUserPO sysUserPO = new SysUserPO();
        sysUserPO.setUsername(sysUserQuery.getUsername());
        sysUserPO.setNickname(sysUserQuery.getNickname());
        sysUserPO.setType(sysUserQuery.getType());
        sysUserPO.setEmail(sysUserQuery.getEmail());
        sysUserPO.setPhone(sysUserQuery.getPhone());
        sysUserPO.setStatus(sysUserQuery.getStatus());
        return sysUserPO;
    }

    public Pagination<SysUserVO> convertToPagination(Page<SysUserPO> sysUserPOPage) {
        Pagination<SysUserVO> pagination = new Pagination<>();
        pagination.setCurrent(sysUserPOPage.getCurrent());
        pagination.setTotal(sysUserPOPage.getTotal());
        pagination.setSize(sysUserPOPage.getSize());
        pagination.setRecords(
                Optional.ofNullable(sysUserPOPage.getRecords())
                        .map(List::stream)
                        .orElseGet(Stream::empty)
                        .map(this::convertToVO)
                        .collect(Collectors.toList()));
        return pagination;
    }

    public SimpleUserVO convertToSimpleUserVO(SysUserPO sysUserPO) {
        SimpleUserVO simpleUserVO = new SimpleUserVO();
        simpleUserVO.setUsername(sysUserPO.getUsername());
        simpleUserVO.setNickname(sysUserPO.getNickname());
        simpleUserVO.setType(sysUserPO.getType());
        simpleUserVO.setEmail(sysUserPO.getEmail());
        simpleUserVO.setPhone(sysUserPO.getPhone());
        simpleUserVO.setGender(sysUserPO.getGender());
        simpleUserVO.setAvatar(sysUserPO.getAvatar());
        simpleUserVO.setStatus(sysUserPO.getStatus());
        simpleUserVO.setRemark(sysUserPO.getRemark());
        entityToVO(sysUserPO, simpleUserVO);
        return simpleUserVO;
    }
}
